package com.stone.customer.core.infra.repository.impl;

import com.stone.customer.core.domain.entity.Complaints;
import com.stone.customer.core.domain.repository.ComplaintsRepository;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 用户投诉建议表 资源库实现
 *
 * @author Mr_wenpan@163.com 2021-07-19 21:54:57
 */
@Slf4j
@Component
public class ComplaintsRepositoryImpl extends BaseRepositoryImpl<Complaints> implements ComplaintsRepository {

    @Resource(name = "pointSyncThreadPool")
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @SneakyThrows
    @Override
    public void threadPoolQuestionTest() {
        AtomicInteger count = new AtomicInteger(0);
        List<Future<AsyncResult<String>>> futureList = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Future<AsyncResult<String>> future = threadPoolTaskExecutor.submit(() -> {
                int k = count.incrementAndGet();
                TimeUnit.SECONDS.sleep(1);
                // 为5时，子线程抛出异常
                if (k == 2) {
                    int j = k / 0;
                }
                log.info("当前线程名：{},当前count={}", Thread.currentThread().getName(), k);
                return new AsyncResult<>("SUCCESS");
            });
            futureList.add(future);
        }

        for (Future<AsyncResult<String>> future : futureList) {
            future.get();
//            try {
//                future.get();
//            } catch (InterruptedException | ExecutionException ex) {
//                log.error("出现异常啦！异常信息：", ex);
//            }
        }
        log.info("主线程结束等待...");
    }
}
